<template>
    <el-cascader  :props="props"
                  v-bind="$attrs"
                  ref="cascader"
                  @change="changeValue"
                  :placeholder="placeholder"
                  v-model="currentVal"
                  v-if="isCurrentVal"
                  >
    </el-cascader>
</template>

<script>
    import { infoTree } from "src/api/cascader";
    export default {
        name: "EdLazyCascader",
        data() {
            return {
                props: {
                    lazy: true,  // 懒加载
                    lazyLoad: this.lazyLoad, //数据方法
                    multiple:this.$attrs.multiple || false, // true多选 false单选,
                    checkStrictly:this.$attrs.checkStrictly || false , // true 任意选择 false 严格父子关联
                    value: "value", // 自定义要映射的键名
                    label: "label",
                },
                placeholder:this.$attrs.placeholder || "小区/分期/楼栋/单元/房间",
                // this.$attrs.leafLevel, // 控制只需要加载到哪个层级 从0开始
                currentVal: [],
                isCurrentVal:true
            };
        },
        created(){

        },
        methods: {
            // 小区 分期 楼栋 单元 房间
            async lazyLoad(node, resolve){
                const { level } = node;
                let nodes = [];
                let query = new Object()
                query.orgId = this.$store.getters.vid;
                switch (level) {
                    // 小区
                    case 0 :
                        query.type = "village";
                        nodes = await infoTree(query).then(response => {
                            if(response.code == 20000) {
                                response.data.forEach((item)=>{
                                    item.label = item.name;
                                    item.value = item.id;
                                    item.leaf = node.level >= this.$attrs.leafLevel
                                });
                                return response.data
                            }
                        });
                        break;
                    // 分期
                    case 1 :
                        query.type = "stages";
                        query.id = node.data.id;
                        query.name = node.data.name;
                        nodes = await infoTree(query).then(response => {
                            if(response.code == 20000) {
                                response.data.forEach((item)=>{
                                    item.label = item.name + "期";
                                    item.value = item.id;
                                    item.leaf = node.level >= this.$attrs.leafLevel
                                });
                                this.sort(response.data)
                                return response.data
                            }
                        });
                        break;
                    // 楼栋
                    case 2 :
                        query.type = "building";
                        query.id = node.data.id;
                        query.name = node.data.name;
                        nodes = await infoTree(query).then(response => {
                            if(response.code == 20000) {
                                response.data.forEach((item)=>{
                                    item.label = item.name + "栋";
                                    item.value = item.id;
                                    item.leaf = node.level >= this.$attrs.leafLevel
                                });
                                this.sort(response.data)
                                return response.data
                            }
                        });
                        break;
                    // 单元
                    case 3 :
                        query.type = "unit";
                        query.id = node.data.id;
                        query.name = node.data.name;
                        nodes = await infoTree(query).then(response => {
                            if(response.code == 20000) {
                                response.data.forEach((item)=>{
                                    item.label = item.name  + "单元";
                                    item.value = item.id;
                                    item.leaf = node.level >= this.$attrs.leafLevel
                                });
                                this.sort(response.data)
                                return response.data
                            }
                        });
                        break;
                    // 房间
                    case 4 :
                        query.type = "room";
                        query.id = node.data.id;
                        query.name = node.data.name;
                        nodes = await infoTree(query).then(response => {
                            if(response.code == 20000) {
                                response.data.forEach((item)=>{
                                    item.label = item.name;
                                    item.value = item.id;
                                    item.leaf = true
                                });
                                this.sort(response.data)
                                return response.data
                            }
                        });
                        break
                }
                resolve(nodes);
            },
            changeValue(val){
                console.log("changeValue",val)
                this.$emit("emit-cascader-value",val);
                this.$emit("input",val);
            },
            // 重置
            reset(){
                this.$refs.cascader.$refs.panel.checkedValue = [];
                this.$refs.cascader.$refs.panel.clearCheckedNodes();
                this.$refs.cascader.$refs.panel.activePath = [];
                this.currentVal = [];
            },
            setVal(val){
                this.isCurrentVal = false;
                this.$nextTick(()=>{
                    this.isCurrentVal = true;
                    this.currentVal = val;
                })
            },
            sort(data){
                data.sort((a,b)=>{
                    const numa = parseFloat(a.name)
                    const numb = parseFloat(b.name)
                    if (isNaN(numa) || isNaN(numb)) {
                        return a.name.localeCompare(b.name)
                    }else {
                        return numa - numb
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>